package service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.mahout.cf.taste.recommender.RecommendedItem;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import model.Answer;
import model.ReadLog;
import utils.UserCF;

public class HomeService {

	public List<Record> getQuestion(String uid){
		List<RecommendedItem> list = UserCF.getCF(Long.valueOf(uid),"qid");
		List<Record> el = Db.find("select qid from read_log where uid = ? ", uid);
		String s = "";
		for(Record r : el){
			s += r.getStr("qid")+",";
		}
		s+="0";
		String from = "from question q left join user u on u.id = q.uid where  q.qid not in ("+s+")";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		if(list == null) {
			String totalRowSql = "select count(*) " + from;
			String findSql = "select * " + from + " order by hot";
			List<Record> l = Db.paginateByFullSql(1, 5, totalRowSql, findSql).getList();
			for (Record record : l) {
				ReadLog r = new ReadLog();
				r.setQid(record.getInt("qid"));
				r.setUid(Integer.valueOf(uid));
				r.setPd((float) 0);
				r.setTime(df.format(new Date()));
				r.save();			
			}
			return l;
		}else if(list.size() < 5) {
			String totalRowSql = "select count(*) " + from;
			String findSql = "select * " + from + " order by hot";
			List<Record> l = Db.paginateByFullSql(1, 5-list.size(), totalRowSql, findSql).getList();
			for (RecommendedItem ritem : list) {
				l.addAll(Db.find("select * "+from, String.valueOf(ritem.getItemID())));
			}
			for (Record record : l) {
				ReadLog r = new ReadLog();
				r.setAid(record.getInt("aid"));
				r.setQid(record.getInt("qid"));
				r.setUid(Integer.valueOf(uid));
				r.setPd((float) 0);
				r.setTime(df.format(new Date()));
				r.save();			
			}
			return l;
		}else {
			List<Record> qlist = new ArrayList<Record>();
			for(RecommendedItem ritem : list){
				qlist.addAll(Db.find("select * "+from));
			}
			for (Record record : qlist) {
				ReadLog r = new ReadLog();
				r.setAid(record.getInt("aid"));
				r.setQid(record.getInt("qid"));
				r.setUid(Integer.valueOf(uid));
				r.setPd((float) 0);
				r.setTime(df.format(new Date()));
				r.save();			
			}
			return qlist;
		}
		
	}
	
	public List<Record> getList(String uid){
		List<RecommendedItem> list = UserCF.getCF(Long.valueOf(uid),"aid");
		List<Record> el = Db.find("select aid from read_log where uid = ? ", uid);
		String s = "";
		for(Record r : el){
			if(r.getStr("aid")!=null)
				s += r.getStr("aid")+",";
		}
		s+="0";
		String from = "from answer a left join question q on q.qid=a.qid left join user u on u.id = a.uid where a.aid not in ("+s+")";
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		if(list == null) {
			String totalRowSql = "select count(*) " + from;
			String findSql = "select * " + from + " order by hot";
			List<Record> l = Db.paginateByFullSql(1, 5, totalRowSql, findSql).getList();
			for (Record record : l) {
				ReadLog r = new ReadLog();
				r.setAid(record.getInt("aid"));
				r.setUid(Integer.valueOf(uid));
				r.setPd((float) 0);
				r.setTime(df.format(new Date()));
				r.save();			
			}
			return l;
		}else if(list.size() < 5) {
			String totalRowSql = "select count(*) " + from;
			String findSql = "select * " + from + " order by a.hot";
			List<Record> l = Db.paginateByFullSql(1, 5-list.size(), totalRowSql, findSql).getList();

			System.out.println(findSql);
			for (RecommendedItem ritem : list) {
				l.addAll(Db.find("select * "+from, String.valueOf(ritem.getItemID())));
			}
			for (Record record : l) {
				ReadLog r = new ReadLog();
				r.setAid(record.getInt("aid"));
				r.setQid(record.getInt("qid"));
				r.setUid(Integer.valueOf(uid));
				r.setPd((float) 0);
				r.setTime(df.format(new Date()));
				r.save();			
			}
			return l;
		}else {
			List<Record> alist = new ArrayList<Record>();
			for(RecommendedItem ritem : list){
				alist.addAll(Db.find("select * "+from));
			}
			for (Record record : alist) {
				ReadLog r = new ReadLog();
				r.setAid(record.getInt("aid"));
				r.setQid(record.getInt("qid"));
				r.setUid(Integer.valueOf(uid));
				r.setPd((float) 0);
				r.setTime(df.format(new Date()));
				r.save();			
			}
			return alist;
		}
		
	}
	
	
	public List<Record> getQuestionByID(String qid){
		List<Record> q = Db.find("select * from question where qid = ? ",qid);
		return q;
		
	}
	public List<Record> getMyQuestion(String uid){
		List<Record> q = Db.find("select * from question q left join user u on u.id = q.uid where uid = ?  ",uid);
		return q;
		
	}
	
	public List<Record> getMyAnswer(String uid){
		List<Record> a = Db.find("select * from answer a left join question q on q.qid=a.qid left join user u on u.id = ? where a.uid = ? ",uid,uid);
		return a;
		
	}
	
	public List<Record> select(String key){
		List<Record> a = Db.find("select * from answer a left join question q on q.qid=a.qid where q.title Like ?","%"+key+"%");
		return a;
		
	}
	
	public List<Record> getInfo(String aid, String uid){
		List<Record> i = Db.find("select *,ifnull(c.id, 0) cc from answer a left join question q on q.qid=a.qid left join collect c on a.aid = c.aid and c.uid = ? where a.aid = ? ",uid,aid);
		return i;
		
	}
	
	
	public List<Record> isDouble(String uid,String aid) {
		return Db.find("select * from read_log where aid = ? and uid = ?",aid,uid);
	}
	
	public List<Record> getCollect(String uid){
		List<Record> i = Db.find("select c.*,q.*,a.* from collect c left join answer a on a.aid = c.aid  left join question q on q.qid=a.qid left join user u on u.id = c.uid where c.uid = ? GROUP BY c.id ",uid);
		return i;
	}
	
	public boolean changePd(int aid,int uid,int pd) {
		ReadLog r = ReadLog.dao.findFirst("select * from read_log where aid = ? and uid = ? ",aid,uid);
		if(r!=null) {
			r.setPd(r.getPd()+pd);
			return r.update();
		}
		return false;
		
	}
}
